package com.zjht.gmcc.servlet.sys;

import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zjht.gmcc.bean.BaseServlet;

import com.zjht.gmcc.bean.util.InitParam;
import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyJDBC;
import com.zjht.gmcc.bean.util.MyParamUtil;

public class OrgaEdit extends BaseServlet {

	private static final long serialVersionUID = -1419654698615505582L;

	public boolean process(HttpServletRequest request, HttpServletResponse response, HttpSession session, PrintWriter out, MyJDBC myJDBC, String menu)
			throws Exception {

		String s_orgatype = request.getParameter("orgaType");
		String s_orgacode = request.getParameter("orgacode");

		int i_flag = 0;
		PreparedStatement ps = null;

		String s_organame = request.getParameter("organame");
		String s_shortname = request.getParameter("shortname");
		String s_linkman = request.getParameter("linkman");
		String s_postcode = request.getParameter("postcode");
		String s_fax = request.getParameter("fax");
		String s_tel = request.getParameter("tel");
		String s_address = request.getParameter("address");
		String s_status = (request.getParameter("status") == null) ? "0" : request.getParameter("status");
		String channelType = request.getParameter("channelType");

		if (s_orgatype == null || s_orgatype.length() == 0) {
			alert(out, "无法更新组织信息，请与管理员联系。");
			return false;
		}

		String s_sql = "";
		boolean isStop = s_status.equals("2");// 是否停用组织

		if ((s_orgatype.equals("4")) || (s_orgatype.equals("83")))// 如果是服务厅或社会渠道厅，则需要更新营业时间等信息
		{

			String s_halltypeid = request.getParameter("halltype");
			String s_fillhour = request.getParameter("fillhour");
			String s_sdate1 = request.getParameter("sdate1");
			String s_sdate2 = request.getParameter("sdate2");
			String s_edate1 = request.getParameter("edate1");
			String s_edate2 = request.getParameter("edate2");

			//修改自有渠道厅(是否自动补货，服务厅类别等)
			if(MyConstant.CHANNEL_GMCC.equals(MyParamUtil.getChannelTypeByOrgaCode(s_orgacode))){
				String autoFlag = request.getParameter("autoFlag");
				s_sql = "update organiza set organame = ?,shortname=?, linkman = ?, postcode = ?, fax = ?, telephone = ?, "
						+ "address = ?, status = ? ,paraid = ?,fillhour = ?,sdate=?,edate=?" + (isStop ? ",stopdate=SYSDATE" : "")
						+ ",halltype = ?,halllevel = ?,autoFlag = ? where orgacode = ?";
				ps = myJDBC.prepareStatement(s_sql);
				ps.setString(1, s_organame);
				ps.setString(2, s_shortname);
				ps.setString(3, s_linkman);
				ps.setString(4, s_postcode);
				ps.setString(5, s_fax);
				ps.setString(6, s_tel);
				ps.setString(7, s_address);
				ps.setString(8, s_status);
				ps.setString(9, "45");
				ps.setString(10, s_fillhour);
				ps.setString(11, s_sdate1 + ":" + s_sdate2);
				ps.setString(12, s_edate1 + ":" + s_edate2);
				ps.setString(13, s_halltypeid);
				ps.setInt(14, 1);
				ps.setString(15, autoFlag);
				ps.setString(16, s_orgacode);				
			}else{
				s_sql = "update organiza set organame = ?,shortname=?, linkman = ?, postcode = ?, fax = ?, telephone = ?, "
					+ "address = ?, status = ? ,paraid = ?,fillhour = ?,sdate=?,edate=?" + (isStop ? ",stopdate=SYSDATE" : "")
					+ " where orgacode = ?";
				ps = myJDBC.prepareStatement(s_sql);
				ps.setString(1, s_organame);
				ps.setString(2, s_shortname);
				ps.setString(3, s_linkman);
				ps.setString(4, s_postcode);
				ps.setString(5, s_fax);
				ps.setString(6, s_tel);
				ps.setString(7, s_address);
				ps.setString(8, s_status);
				ps.setString(9, s_halltypeid);
				ps.setString(10, s_fillhour);
				ps.setString(11, s_sdate1 + ":" + s_sdate2);
				ps.setString(12, s_edate1 + ":" + s_edate2);
				ps.setString(13, s_orgacode);				
			}	
		} else {
			s_sql = "update organiza set organame = ?, shortname=?, linkman = ?, postcode = ?, fax = ?, telephone = ?, " + "address = ?, status = ? "
					+ (isStop ? ",stopdate=SYSDATE" : "") + " where orgacode = ?";
			ps = myJDBC.prepareStatement(s_sql);
			ps.setString(1, s_organame);
			ps.setString(2, s_shortname);
			ps.setString(3, s_linkman);
			ps.setString(4, s_postcode);
			ps.setString(5, s_fax);
			ps.setString(6, s_tel);
			ps.setString(7, s_address);
			ps.setString(8, s_status);
			ps.setString(9, s_orgacode);
		}
		i_flag = ps.executeUpdate();
		ps.close();

		if (i_flag == 1 && s_orgatype.equals("4")) {// 修改入柜商厅信息，需要修改mobiletorgs，入柜商厅与自有渠道的对应关系
			s_sql = "SELECT channeltype FROM organiza WHERE orgacode = ? ";
			ps = myJDBC.prepareStatement(s_sql);
			ps.setString(1, s_orgacode);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				if (rs.getString("channeltype").equals(MyConstant.CHANNEL_RGS)) {
					channelType = MyConstant.CHANNEL_RGS;
					String gmccHall = request.getParameter("gmccHall");

					s_sql = "MERGE INTO mobiletorgs a USING (SELECT ? AS rgscode FROM DUAL) b " + "ON (a.rgscode = b.rgscode) "
							+ "WHEN MATCHED THEN UPDATE SET a.mobilecode = ? " + "WHEN NOT MATCHED THEN INSERT (mobilecode, rgscode) "
							+ "VALUES(?, ?)";
					// s_sql = "update mobiletorgs set mobilecode = ? where rgscode = ? ";
					ps = myJDBC.prepareStatement(s_sql);
					ps.setString(1, s_orgacode);
					ps.setString(2, gmccHall);
					ps.setString(3, gmccHall);
					ps.setString(4, s_orgacode);
					i_flag = ps.executeUpdate();
					ps.close();
				}
			}
		}

		String act = s_organame + "修改";
		if (i_flag > 0) {
			InitParam.initOrgaInfo();
			alert(out, act + "成功", "/sys/orga/orga.jsp?orgacode=" + s_orgacode + "&orgaType=" + s_orgatype + "&channelType=" + channelType);
		} else {
			errAlert(request, out, menu, act + "失败");
		}
		return i_flag > 0;
	}
}
